const mongodb = require("mongodb");
const mongoClient = mongodb.MongoClient;
// 私有函数。
function _connect() {
    return new Promise((resolve,reject)=>{
        mongoClient.connect("mongodb://127.0.0.1",{ useUnifiedTopology: true},function (err,client) {
            if(err) reject(err);
            else resolve(client.db("hungry"))
        })
    })
}
module.exports.insertOne = async function (collName,insertObj={}) {
    const db = await _connect();
    return new Promise((resolve,reject)=>{
        db.collection(collName).insertOne(insertObj,(err,results)=>{
            if(err) reject(err);
            else resolve(results);
        })
    })
}

// const obj = {
//     countDocuments(){
//         const num = 12;
//         return {
//             then(fn){
//                 fn(num);
//             }
//         }
//     }
// }

/*
* 根据条件获得文档的数量
* collName:集合名
* whereObj:条件
* */
module.exports.count = async function (collName,whereObj={}) {
    const db = await _connect();
    return new Promise((resolve,reject)=>{
        db.collection(collName).countDocuments(whereObj).then(count=>{
             resolve(count);
        })
    })
}
// 查找多条
module.exports.find = async function (collName, { whereObj = {}, skip = 0, limit = 0, sortObj = {} }) {
    const db = await _connect();
    return new Promise((resolve, reject) => {
        db.collection(collName).find(whereObj).limit(limit).skip(skip).sort(sortObj).toArray(function (err, results) {
            if (err) reject(err)
            else resolve(results);
        })
    })
}



module.exports.findOne=async function (collName,whereObj) {
    const db=await _connect()
    return new Promise((resolve,reject)=>{
        db.collection(collName).findOne(whereObj,(err,results)=>{
            if(err){
                reject(err)
            }else{
                resolve(results)
            }
        })
    })
}
module.exports.findOneById=async function (collName,id) {
    const db=await _connect()
    return new Promise((resolve,reject)=>{
        db.collection(collName).findOne({_id:mongodb.ObjectId(id)},(err,results)=>{
            if(err){
                reject(err)
            }else{
                resolve(results)
            }
        })
    })
}
module.exports.delete=async function(collName,whereObj){
    const db=await _connect();
    return new Promise((resolve,reject)=>{
        db.collection(collName).remove(whereObj,(err,results)=>{
            if(err){
                reject(err)
            }else{
                resolve(results)
            }
        })
    })
}

module.exports.deleteOneById = async function (collName,id) {
    const db = await _connect();
    return new Promise((resolve,reject)=>{
        db.collection(collName).deleteOne({_id:mongodb.ObjectId(id)},function (err,results) {
            if(err) reject(err);
            else resolve(results);
        })
    })
}
module.exports.updateOne = async function (collName,whereObj,upObj) {
    const db = await _connect();
    return new Promise((resolve,reject)=>{
        db.collection(collName).updateOne(whereObj,upObj,function (err,results) {
            if(err) reject(err);
            else resolve(results);
        })
    })
}

module.exports.updateOneById = async function (collName,id,upObj) {
    const db = await _connect();
    return new Promise((resolve,reject)=>{
        db.collection(collName).updateOne({_id:mongodb.ObjectId(id)},upObj,function (err,results) {
            if(err) reject(err);
            else resolve(results);
        })
    })
}
// module.exports.count("contextList",{context:"谢老师去日本玩啦！"});